﻿using System;
using Alserve.Ffl.Data;

public partial class ffl_user_webusercontrols_edituserprofile : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateUserProfile();
        }
    }

    protected void EditProfileButton_OnClick(object sender, EventArgs e)
    {
        if (ValidateProfileChanges())
        {
            User thisUser = new User();
            thisUser.Where.Id.Value = Convert.ToInt32(Session["UserID"]);

            if (thisUser.Query.Load())
            {
                thisUser.Forename = ForenameTextBox.Text.Trim();
                thisUser.Surname = SurnameTextBox.Text.Trim();
                thisUser.TeamName = TeamNameTextBox.Text.Trim();
                thisUser.Save();
                SuccessLabel.Text = "Your profile has been updated successfully.";
                SuccessLabel.Visible = true;
                EditProfileTable.Visible = false;
                ChangePasswordTable.Visible = false;
            }
        }
    }

    protected void ChangePasswordButton_OnClick(object sender, EventArgs e)
    {
        if (ValidatePasswordChange())
        {
            User thisUser = new User();
            thisUser.Where.Id.Value = Convert.ToInt32(Session["UserID"]);

            if (thisUser.Query.Load())
            {
                thisUser.Password = PasswordTextBox.Text.Trim();
                thisUser.Save();
                SuccessLabel.Text = "Your password has been changed successfully.";
                SuccessLabel.Visible = true;
                EditProfileTable.Visible = false;
                ChangePasswordTable.Visible = false;
            }
        }
    }

    private Boolean ValidateProfileChanges()
    {
        Boolean returnValue = true;
        ErrorTextLabel.Text = "";
        if (ForenameTextBox.Text.Length == 0)
        {
            ErrorTextLabel.Text += "Please enter a forename.<br/>";
            returnValue = false;
        }

        if (SurnameTextBox.Text.Length == 0)
        {
            ErrorTextLabel.Text += "Please enter a surname.<br/>";
            returnValue = false;
        }

        if (TeamNameTextBox.Text.Length == 0)
        {
            ErrorTextLabel.Text += "Please enter a team name.</br>";
            returnValue = false;
        }

        return returnValue;
    }

    private Boolean ValidatePasswordChange()
    {
        Boolean returnValue = true;
        PasswordErrorTextLabel.Text = "";
        
        if (PasswordTextBox.Text.Length < 6)
        {
            PasswordErrorTextLabel.Text += "Password must be at least 6 characters.<br/>";
            returnValue = false;
        }

        if (PasswordTextBox.Text != ConfirmPasswordTextBox.Text)
        {
            PasswordErrorTextLabel.Text += "Passwords do not match.<br/>";
            returnValue = false;
        }

        return returnValue;
    }

    private void PopulateUserProfile()
    {
        User thisUser = new User();
        thisUser.Where.Id.Value = Convert.ToInt32(Session["UserID"]);
        
        if (thisUser.Query.Load()) 
        {
            ForenameTextBox.Text = thisUser.Forename;
            SurnameTextBox.Text = thisUser.Surname;
            TeamNameTextBox.Text = thisUser.TeamName;
            EmailTextBox.Text = thisUser.Email;
        }
    }

}